const jwt=require("jsonwebtoken");
module.exports={
    author(){
        return (req,res,next)=>{
            let token="";
            if (req.headers['authorization']) {
                token=req.headers['authorization'].split('Bearer ')[1];
            }else{
                token=req.query.token||req.body.token||req.headers['token']||
                req.cookies['token']
            }
            console.log("token:"+token);
            //解析token
           try {
            jwt.verify(token, "cs2209", function(err, decoded){
                console.log(decoded);
                if(err){
                    console.log(err);  //会输出123，如果过了60秒，则有错误。
                    throw err
                }
                next();
            });
           } catch (error) {
                res.json({
                    msg:"token无效,请重新登陆",
                    error,
                    code:403
                })
           }
        }
    }
}